class PagesController < ApplicationController
  
  layout "pages", :except => :edit
  
#  scaffold :page, :suffix => true
#  scaffold :content, :suffix => true
  
  
  def set_global_settings_site_name
    begin
      @setting = GlobalSettings[:site_name]
      @setting.value = params[:global_settings][:site_name]
      @setting.save
      render :text => @setting.value
    rescue
      render :text =>""
    end
  end
  in_place_edit_for :page, :title
  in_place_edit_for :global_settings, :value
  
  def index
  end
  
  def list
    @global_settings = GlobalSettings[:site_name]
    @pages = Page.find_all
  end
  
  def create
    first_page = Page.find(:first) ? false : true
    @page = Page.create(:title => "New Page (#{ ((Page.find(:all).last ? Page.find(:all).last.id : 0) + 1 ).to_s})",
                        :theme_name => Page.available_themes.first )
    if @page.save
      render :update do |page|
        page.insert_html :bottom, 'pages_listed', :partial=>'page_listed'
        page["page_"<< @page.id.to_s].hide
        page["page_"<< @page.id.to_s].visual_effect :slide_down
        if first_page
          page.hide 'blank_slate', 'clickhere'
        end
      end
    else
      render :update do |page|
        page.replace_html 'flash_error', "The page title you've chosen is unavailable, please try with a diferent title."
      end
    end
  end
  
  def destroy
    @page = Page.find(params[:id])
    @page.destroy
    render :update do |page|
      unless Page.find :first
        page.show 'blank_slate', 'clickhere'
      end
      page['page_'+params[:id]].visual_effect :drop_out
    end
  end
  
  def edit
    @page = Page.find(params[:id])
  end
  
  def select_theme
    @page = Page.find(params[:id])
    render :update do |page|
      page.insert_html :bottom, "change_theme", :partial => "select_theme"
    end
  end
  
  def save_theme
    @page = Page.find(params[:id])
    @page.update_attribute :theme_name, params[:page][:theme_name]
    
    redirect_to :action=>"edit", :id=>params[:id]
  end
  
end
